<template>
  <LoginHeader>联合登录</LoginHeader>
  <section class="container" v-if="loading">
    <div class="unbind">
      <div class="loading"></div>
    </div>
  </section>
  <section class="container" v-else>
    <nav class="tab">
      <a
        @click="hasAccount = true"
        :class="{ active: hasAccount }"
        href="javascript:;"
      >
        <i class="iconfont icon-bind" />
        <span>已有小兔鲜账号，请绑定手机</span>
      </a>
      <a
        @click="hasAccount = false"
        :class="{ active: !hasAccount }"
        href="javascript:;"
      >
        <i class="iconfont icon-edit" />
        <span>没有小兔鲜账号，请完善资料</span>
      </a>
    </nav>
    <div class="tab-content" v-if="hasAccount">
      <CallbackBind :unionId="unionId" />
    </div>
    <div class="tab-content" v-else>
      <CallbackPatch />
    </div>
  </section>
  <LoginFooter />
</template>

<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import LoginHeader from './components/login-header'
import LoginFooter from './components/login-footer'
import CallbackBind from './components/callback-bind'
import CallbackPatch from './components/callback-patch'
import { loginSocial } from '@/api/user'
import qc from 'qc'
export default {
  name: 'PageCallback',
  components: { LoginHeader, LoginFooter, CallbackBind, CallbackPatch },
  setup () {
    /*
qq登陆后的回跳页面
   1：已有小兔鲜帐号，且和登陆的qq绑定过：获取相应信息验证后立马跳转到对应小兔鲜页面，如果验证失败，显示我们callback页面
      a:获取qq返回给我们的相应信息（openId）
      b:拿该openid去后端接口验证，验证该openid有没有绑定过小兔鲜帐号
      c:验证成功，正常登陆成功
      d:验证失败，进入下面的2、3进行处理



   2：qq登陆了，有小兔鲜帐号，但qq和小小兔鲜没有绑定过
   3：qq登陆了，没有小兔鲜帐号，

*/

    const hasAccount = ref(true)
    const loading = ref(true)
    const unionId = ref('')
    const router = useRouter()
    const store = useStore()
    if (qc.Login.check()) {
      qc.Login.getMe(openid => {
        unionId.value = openid
        console.log('qq的openid:', openid)
        loginSocial({
          unionId: openid,
          source: 6
        })
          .then(res => {
            //  该qq帐号与小免鲜绑定过,跳转到来的页面
            store.commit('user/setUserInfo', res.result)
            router.push(store.state.user.redirect)
          })
          .catch(() => {
            // 该qq帐号与小免鲜没绑定过
            loading.value = false
          })
      })
    }

    return { hasAccount, loading, unionId }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: @xtxColor;
      border-color: @xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
.container {
  padding: 25px 0;
  position: relative;
  height: 730px;
  .unbind {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    padding: 25px 0;
    z-index: 99;
    .loading {
      height: 100%;
      background: #fff url(~@/assets/images/load.gif) no-repeat center / 100px
        100px;
    }
  }
}
</style>
